#--------------------------------------------------------
#  configuration
#--------------------------------------------------------
degree = 5
dataDir = "C:/ayoub/fim/data/SPY/"
dataFile = "/march.csv"
plotFile = 'skew3.png'

#--------------------------------------------------------
#  imports
#--------------------------------------------------------
import numpy as np
import matplotlib.pyplot as plt
import extensions as ext
from math import log

#--------------------------------------------------------
#  OptionType enum
#--------------------------------------------------------
class OptionType:
    Call, Put = range(2)

#--------------------------------------------------------
#  strike2Moneyness
#--------------------------------------------------------
def strike2Moneyness(spot,inputArr,base = 2, option_type = OptionType.Call):
    """convert strike values to moneyness
       input: spot, array, [base]"""
    if (option_type == OptionType.Call):
       result = [log(float(spot)/float(val)) for val in inputArr]
    else:
      result = [log(val/spot) for val in inputArr]
    return result

#--------------------------------------------------------
#  leastSquareFit
#--------------------------------------------------------
def leastSquareFit(x,y):
    """return fitted curve y(x) of degree 5.
       expect input arrays and returns a poly1d"""
    degree = 5

    # form the Vandermonde matrix
    A = np.vander(x,degree)

    # find the x that minimizes the norm of Ax-y
    (coeffs, residuals, rank, sing_vals) = np.linalg.lstsq(A, y)

    # create a polynomial using coefficients
    #f = np.poly1d(coeffs)
    return np.poly1d(coeffs)


"""
def getSkewCurve(spot, arr):
    "returns skew given a spot and option prices"
    leastSquareFit(x,y)
    

ta = np.transpose(arr)

moneyness = ta[0]
price = ta[1]


# for plot, estimate y for each observation time
y_est = f(moneyness)

# create plot
plt.plot(moneyness, price, '.', label = 'original data', markersize=5)
plt.plot(moneyness, y_est, 'o-', label = 'interpolation', markersize=1)
plt.xlabel('moneyness')
plt.ylabel('price')
plt.title('least squares fit of degree 5')
plt.savefig(dataDir + plotFile)

"""
